package com.homework;

/**
 * @author: XiangHu
 * @date: 2025年02月24日 17:21
 * @describe:
 */


import java.sql.*;
import java.util.Scanner;


public class Zuoye {

    
    static String url = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8";
    static String user = "root";
    static String password = "123456";


    public static Connection getConnection() throws SQLException {
        try {

            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }
    }


    public static boolean login(String username, String password) {
        String sql = "SELECT * FROM t_user WHERE username = ? AND password = ?";
        try (Connection connection = getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next())
            {
                System.out.println("登录成功！欢迎 " + username);
                return true;
            } else
            {
                System.out.println("用户名或密码错误！");
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public static void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n请选择操作:");
            System.out.println("(1) 显示所有学生");
            System.out.println("(2) 添加学生");
            System.out.println("(3) 删除学生");
            System.out.println("(4) 更新学生");
            System.out.println("(0) 退出");

            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    showAllStudents();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    updateStudent();
                    break;
                case 0:
                    System.out.println("退出系统成功！");
                    return;
                default:
                    System.out.println("无效选项，请重新选择！");
            }
        }
    }

    
    public static void showAllStudents() {
        String sql = "SELECT * FROM student";
        try (Connection conn = getConnection()) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") +
                        ", Age: " + rs.getInt("age") + ", Cellphone: " + rs.getString("cellphone") +
                        ", Address: " + rs.getString("address") + ", Gender: " + rs.getString("gender"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    
    public static void addStudent() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();

        System.out.print("请输入学生年龄: ");
        int age = scanner.nextInt();

        scanner.nextLine();
        System.out.print("请输入学生手机号: ");
        String cellphone = scanner.nextLine();

        System.out.print("请输入学生地址: ");
        String address = scanner.nextLine();

        System.out.print("请输入学生性别: ");
        String gender = scanner.nextLine();

        String sql = "insert into student (name, age, cellphone, address, gender) values(?, ?, ?, ?, ?)";
        try (Connection conn = getConnection()
             ) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, name);
            stmt.setInt(2, age);
            stmt.setString(3, cellphone);
            stmt.setString(4, address);
            stmt.setString(5, gender);

            stmt.executeUpdate();
            System.out.println("学生添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    
    public static void deleteStudent() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要删除的学生ID: ");
        int studentId = scanner.nextInt();

        String sql = "DELETE FROM student WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql))
        {

            stmt.setInt(1, studentId);
            int rowsAffected = stmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("学生删除成功！");
            } else {
                System.out.println("未找到学生！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    
    public static void updateStudent() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要更新的学生ID: ");
        int studentId = scanner.nextInt();
        scanner.nextLine(); 

        System.out.print("请输入新的学生姓名: ");
        String name = scanner.nextLine();

        System.out.print("请输入新的学生年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); 

        System.out.print("请输入新的学生手机号: ");
        String cellphone = scanner.nextLine();

        System.out.print("请输入新的学生地址: ");
        String address = scanner.nextLine();

        System.out.print("请输入新的学生性别: ");
        String gender = scanner.nextLine();

        String sql = "UPDATE student SET name = ?, age = ?, cellphone = ?, address = ?, gender = ? WHERE id = ?";
        try (Connection conn = getConnection()) {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, name);
            stmt.setInt(2, age);
            stmt.setString(3, cellphone);
            stmt.setString(4, address);
            stmt.setString(5, gender);
            stmt.setInt(6, studentId);

            int rowsAffected = stmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("学生信息更新成功！");
            } else {
                System.out.println("未找到学生！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        if (login(username, password)) {
            showMenu();
        }

        scanner.close();
    }
}

